<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>

</head>
<body>
<div class="layui-fluid" id="flowDiv">

    <div class="right-content" style="float: left; padding-left:1%;width: 98%">
        <div class="layui-inline">
            <label class="layui-form-label">公司名称</label>
            <div class="layui-input-inline">
                <input type="text" id="name" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" onclick="search()">查询</button>
            </div>
        </div>
        <table class="layui-table" id="datatable" lay-filter="test"></table>
    </div>
</div>
</body>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="relatePerson">人员</a>
</script>

<SCRIPT type="text/javascript">
    var form,tableObj,table,level=0,treeNodeId=-1,selecNode,count=1,index_i1,element,upload,type,companyTypeMap,stateGlo;
    $(function(){
        layui.use(['form','table','element','upload'], function(){
            element = layui.element;
            form = layui.form;
            table = layui.table;
            upload = layui.upload;
            //loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
            type = window.parent.typeGlo;
            loadTable(type);
        });
        companyTypeMap=getDic("CompanyType");
        stateGlo = getDic("OrgStatus");
    });

    function searchData(){

    }
    //加载表格
    function loadTable(type){
        var tableurl = publicUrl+'resExternal/getResExternalCompany?type='+type;
            tableObj = table.render({
                elem: '#datatable',
                //size: 'sm',
                even: true, //开启隔行背景
                height: 'full-100',
                method: 'post',
                url: tableurl,
                headers: {"Authorization":getData("Authorization")},
                where:{
                },
                response: {
                    statusCode: 2000
                },
                parseData: function(res){ //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "count": res.data.total, //解析数据长度
                        "msg": res.msg, //解析提示文本
                        "data": res.data.list //解析数据列表
                    };
                },
                page: true,
                cols: [[
                    {
                        type:'radio',
						width: 40,
						align: 'center'
                    },
                    {
                        type: 'numbers',
                        title: '序号',
						width: 60,
                        align: 'center'
                    }, {
                        field: 'name',
                        title: '名称',
                        sort: true,
						 width: "25%",
                        align: 'center'
                    },{
                        field: 'type',
                        title: '类型',
                        sort: false,
						width: "12%",
                        align: 'center',
                        templet: function(d) {
                            return companyTypeMap.get(d.type+"");
                        }
                    }, {
                        field: 'state',
                        title: '状态',
                        sort: false,
						width: "10%",
                        align: 'center',
                        templet: function(d) {
                            return stateGlo.get(d.state+"");
                        }
                    },/*{
                        field: 'phone',
                        title: '电话',
                        sort: true,
                        align: 'center'
                    },{
                        field: 'postalCode',
                        title: '邮编',
                        sort: true,
                        align: 'center'
                    },{
                        field: 'webSite',
                        title: '网站',
                        align: 'center'
                    },*/{
                        field: 'address',
                        title: '地址',
                        sort: true,
						width: "35%",
                        align: 'center'
                    }]],
                limit: 20
            });

        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'create':
                    create();
                    break;
                case 'edit':
                    var dataEdit = checkStatus.data;
                    edit(dataEdit);
                    break;
                case 'deleteDel':
                    var dataDel = checkStatus.data;
                    deleteDel(dataDel);
                    break;
                case 'importData':
                    uploadResLabours();
                    break;
                case 'relatePerson':
                    var node = checkStatus.data;
                    relatePerson(node[0]);
                    break;
                case 'search':
                    search();
                    break;
            }
        });

            table.on('tool(test)', function(obj){
                var node = obj.data;
                switch(obj.event){
                    case 'relatePerson':
                        relatePerson(node);
                        break;
                }
            });
    }

    //查询
    //查询
    function search(){
        var data = new Object();
        var type = $.trim($('#type').val());
        var name = $.trim($('#name').val());
        if(type!==''){
            data.type = type;
        }
        if(name!==''){
            data.name = name;
        }
        if(name === '' && type === ''){
            reloadTable('datatable',null);
        }
        reloadTable('datatable',data);
    }



    //关联人员
    function relatePerson(seletedNode){
        if(typeof (seletedNode) != 'undefined' && seletedNode != null){
            var index = layer.open({
                type: 2,
                area: ["500px" , "350px"],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: "关联人员",
                content: "relateperson.html",
                btn: ['确定'],
                // 弹层外区域关闭
                shadeClose: false,
                yes: false,
                cancel: function(index) {
                    return true;
                },
                success: function(layero, index){
                    iframeWin = layero.find('iframe')[0];
                    window[layero.find('iframe')[0]['name']].initCompany(seletedNode);
                }
            });
            layer.full(index);
        }else{
            layer.alert("请选择一条记录");
        }
    }


    var iframeWin;
    //修改
    function edit(){
        var dataEdit = layui.table.checkStatus('datatable').data;
        if(typeof(dataEdit)!=='undefined' && dataEdit!==null){
            if(dataEdit.length >0 ){
                var index = layer.open({
                    type: 2,
                    title: '新建外部单位',
                    //			maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    area: ['790px', '800px'],
                    content: 'addresexternal.html',
                    // content: $("#addcom"),
                    btn: ['确定','取消'],
                    maxmin: true,
                    success: function(layero, index) {
                        iframeWin = layer.getChildFrame('#reslabourform', index);//.initForm(dataEdit[0]);
                        window[layero.find('iframe')[0]['name']].initForm(dataEdit[0]);
                    },
                    btn1: function(index, layero) {
                        submitcreate('resExternal/insertOrUpdate');
                        index_i1 = index;
                        return false;
                    },
                    btn2: function(index, layero) {
                        form.val("reslabourform", {
                            name: '',
                            code: '',
                            type: '',
                            registeredCapital: '',
                            address: '',
                            registrar: '',
                            businessScope: ''
                        })
                    },
                    cancel: function(index, layero) {
                        form.val("reslabourform", {
                            name: '',
                            code: '',
                            type: '',
                            registeredCapital: '',
                            address: '',
                            registrar: '',
                            businessScope: ''
                        })
                    }
                });
            }else{
                layer.alert('请选择要修改的内容');
            }
        }
    }


    //新建
    function create(){
        var index = layer.open({
            type: 2,
            title: '新建劳务公司',
            //			maxmin: true,
            shadeClose: false, //点击遮罩关闭层
            area: ['790px', '800px'],
            content: 'addresexternal.html',
            // content: $("#addcom"),
            btn: ['应用','完成','取消'],
            maxmin: true,
            success: function(layero, index) {
                iframeWin = layer.getChildFrame('#reslabourform', index);
            },
            yes: function(index, layero) {
               submitcreate('resExternal/insertOrUpdate');
            },
            btn2: function(index, layero) {
                submitcreate('resExternal/insertOrUpdate');
                index_i1 = index;
                return false;
            },
            btn3: function(index, layero) {
                form.val("reslabourform", {
                    name: '',
                    code: '',
                    type: '',
                    registeredCapital: '',
                    address: '',
                    registrar: '',
                    businessScope: ''
                })
            },
            cancel: function(index, layero) {
                form.val("reslabourform", {
                    name: '',
                    code: '',
                    type: '',
                    registeredCapital: '',
                    address: '',
                    registrar: '',
                    businessScope: ''
                })
            }
        });
        //layer.full(index);
    }


    function submitcreate(url) {
        var data = iframeWin.serializeJson();
        if(typeof(data) != 'undefined' && data != null){
            publicAjax (null,url,{"id":data},null,null,function (r) {
                if(r.code==2000){
                    form.val("reslabourform", {
                        name: '',
                        code: '',
                        type: '',
                        registeredCapital: '',
                        address: '',
                        registrar: '',
                        businessScope: ''
                    });
                    loadTable();
                    if(index_i1) {
                        layer.close(index_i1);
                    }else{

                    }
                }else{
                    layer.alert(r.msg);
                }
            });
            /*$.ajax({
                type:"post",
                url: "/mdm/" + url,
                async:true,
                data:data,
                success: function (data){
                    if(data.code == 2000){
                        form.val("reslabourform", {
                            name: '',
                            code: '',
                            type: '',
                            registeredCapital: '',
                            address: '',
                            registrar: '',
                            businessScope: ''
                        });
                        reloadTable("datatable",null);
                        if(index_i1) {
                            layer.close(index_i1);
                        }else{

                        }
                    }else{
                        layer.alert(data.msg);
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    if(XMLHttpRequest.status=="401"){
                        layer.msg(XMLHttpRequest.responseText,{icon:5},function(){
                            window.location.href = "/authentication/login.html";
                        })
                    }
                }
            });*/
        }
    }

    function reloadTable(tableId,data){
        table.reload(tableId, { //此处是上文提到的 初始化标识id
            where: data
        });
    }

    /* *
     * @Author bxf
     * @Description 删除记录
     * @Date 11:19 2019/7/23
     * @param null
     * @return 
     */
    function  deleteDel() {
        var deleteDel = layui.table.checkStatus('datatable').data;
        var idStr ="";
        //获取选中项
        if(!deleteDel || deleteDel.length<1){
            layer.alert("请至少选择一项信息！");
            return false;
        }
        for(var i=0;i<deleteDel.length;i++){
            idStr = idStr + "," + deleteDel[i].id;
        }
        layui.use(['layer'],function(){
            var layer = layui.layer;
            layer.confirm('确定删除？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function(index){
                var delurl = '/resExternal/delete';
                /*$.ajax({
                    type:"post",
                    url: "/mdm/" + delurl,
                    async:true,
                    data:{"id":idStr},
                    success: function (data){
                        if(data.code == 2000){
                            layer.close(index);
                            reloadTable("datatable",null);
                        }else{
                            layer.alert(data.msg);
                        }
                    }
                });*/
                publicAjax (null,delurl,{"id":idStr},null,null,function (r) {
                    if(r.code==2000){
                        layer.close(index);
                        reloadTable("datatable",null);
                    }else{
                        layer.alert(r.msg);
                    }
                });
            }, function(){
                layer.closeAll();
            });
        })
    }

    //导入
    function uploadResLabours(){
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["500px" , "350px"],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "劳务公司导入",
            content: "uploadresexternal.html",
            btn: ['关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: false,
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
               /* iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.getdisciplineId(nodes);//添加按钮*/
            }
        });
    }


</script>
</html>